package com.fjwt.gz.mgr.ctrl.score.rule;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.entity.AgentScoreRuleEntity;
import com.fjwt.gz.db.entity.SysConfig;
import com.fjwt.gz.db.vo.AgentScoreRuleVO;
import com.fjwt.gz.db.vo.ScoreRuleAndTotalVO;
import com.fjwt.gz.mgr.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.AgentScoreRuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  积分规则接口类
 */
@RestController
@RequestMapping("/api/scoreRule")
@Slf4j
public class ScoreRuleController extends CommonCtrl {

    @Autowired
    private AgentScoreRuleService agentScoreRuleService;



    @PostMapping("/getSysConfig")
    public ApiRes getSysConfig(){
        List<SysConfig> list = agentScoreRuleService.getSysConfig();
        log.info("pc端获取系统配置表，请求地址：/api/scoreRule/getSysConfig，返回结果：{}", JSONObject.toJSONString(list));
        return ApiRes.ok(list);
    }

    /**
     * 积分规则列表
     */
    @PreAuthorize("hasAuthority('ENT_SCORE_RULE_INFO')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes getScoreRule(){
        AgentScoreRuleVO object = getObject(AgentScoreRuleVO.class);
        log.info("pc端积分规则列表，请求地址：/api/scoreRule，请求参数：{}", JSONObject.toJSONString(object));
        IPage<ScoreRuleAndTotalVO> page = agentScoreRuleService.findScoreRuleLists(getIPage(), object);
        log.info("pc端积分规则列表，请求地址：/api/scoreRule，返回结果：{}", JSONObject.toJSONString(page));
        return ApiRes.page(page);
    }

    /**
     * 积分规则新增
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_RULE_INFO_ADD')")
    @PostMapping("/save")
    public ApiRes scoreRuleSave(){
        AgentScoreRuleEntity entity = getObject(AgentScoreRuleEntity.class);
        log.info("pc端积分规则新增，请求地址：/api/scoreRule/save，请求参数：{}", JSON.toJSONString(entity));
        int result = agentScoreRuleService.scoreRuleSave(entity);
        if(result > 0){
            return ApiRes.ok();
        }else{
            return ApiRes.customFail("操作失败！");
        }
    }

    /**
     * 获取积分规则详情
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_RULE_INFO_VIEW')")
    @PostMapping("/getInfo")
    public ApiRes getInfo(){
        String scoreRuleId = getValString("scoreRuleId");
        log.info("pc端积分规则详情，请求地址：/api/scoreRule/getInfo，请求参数：{}", scoreRuleId);
        ScoreRuleAndTotalVO vo = agentScoreRuleService.queryRuleAndTotalByRuleId(scoreRuleId);
        log.info("pc端积分规则详情，请求地址：/api/scoreRule/getInfo，返回结果：{}", JSON.toJSONString(vo));
        return ApiRes.ok(vo);
    }

    /**
     * 修改积分规则
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_RULE_INFO_EDIT')")
    @PostMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public ApiRes update(){
        ScoreRuleAndTotalVO entity = getObject(ScoreRuleAndTotalVO.class);
        log.info("pc端积分规则修改，请求地址：/api/scoreRule/update，请求参数：{}", JSON.toJSONString(entity));
        int result = agentScoreRuleService.updateScoreRule(entity);
        if(result > 0){
            return ApiRes.ok();
        }else{
            return ApiRes.customFail("操作失败！");
        }
    }

//    /**
//     * 删除积分规则
//     */
//    @PostMapping("/remove")
//    public ApiRes remove(){
//        AgentScoreRuleEntity entity = getObject(AgentScoreRuleEntity.class);
//        log.info("pc端积分规则删除，请求地址：/api/scoreRule/remove，请求参数：{}", JSON.toJSONString(entity));
//        int result = agentScoreRuleService.deleteById(entity.getScoreRuleId());
//        if(result > 0){
//            return ApiRes.ok();
//        }else{
//            return ApiRes.customFail("操作失败！");
//        }
//    }

    /**
     * 积分规则管理状态变更（启用/不启用）
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_SCORE_RULE_INFO_EDIT')")
    @PostMapping("/updateState")
    public ApiRes updateState(){
        AgentScoreRuleEntity entity = getObject(AgentScoreRuleEntity.class);
        log.info("pc端积分规则管理状态变更，请求地址：/api/scoreRule/updateState，请求参数：{}", JSON.toJSONString(entity));
        Boolean flag = agentScoreRuleService.updateStateById(entity);
        return flag ? ApiRes.ok() : ApiRes.customFail("操作失败！");
    }

}
